home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / syck / loaders.pyo (.txt) < prev   
Python Compiled Bytecode  |  2008-10-13  |  14KB  |  486 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import generators
  5.  
  6. try:
  7.     import datetime
  8. except ImportError:
  9.     pass
  10.  
  11.  
  12. try:
  13.     Set = set
  14. except:
  15.     
  16.     try:
  17.         from sets import Set
  18.     except ImportError:
  19.         
  20.         def Set(items):
  21.             set = { }
  22.             for items in items:
  23.                 set[items] = None
  24.             
  25.             return set
  26.  
  27.  
  28.  
  29. import _syck
  30. import sys
  31. import re
  32. import warnings
  33. __all__ = [
  34.     'GenericLoader',
  35.     'Loader',
  36.     'parse',
  37.     'load',
  38.     'parse_documents',
  39.     'load_documents',
  40.     'NotUnicodeInputWarning']
  41.  
  42. class NotUnicodeInputWarning(UserWarning):
  43.     pass
  44.  
  45.  
  46. class GenericLoader(_syck.Parser):
  47.     
  48.     def load(self):
  49.         node = self.parse()
  50.         if self.eof:
  51.             return None
  52.         
  53.         return self._convert(node, { })
  54.  
  55.     
  56.     def _convert(self, node, node_to_object):
  57.         if node in node_to_object:
  58.             return node_to_object[node]
  59.         
  60.         value = None
  61.         if node.kind == 'scalar':
  62.             value = node.value
  63.         elif node.kind == 'seq':
  64.             value = []
  65.             for item_node in node.value:
  66.                 value.append(self._convert(item_node, node_to_object))
  67.             
  68.         elif node.kind == 'map':
  69.             value = { }
  70.             for key_node in node.value:
  71.                 key_object = self._convert(key_node, node_to_object)
  72.                 value_object = self._convert(node.value[key_node], node_to_object)
  73.                 
  74.                 try:
  75.                     if key_object in value:
  76.                         value = None
  77.                         break
  78.                     
  79.                     value[key_object] = value_object
  80.                 continue
  81.                 except TypeError:
  82.                     value = None
  83.                     break
  84.                     continue
  85.                 
  86.  
  87.             
  88.             if value is None:
  89.                 value = []
  90.                 for key_node in node.value:
  91.                     key_object = self._convert(key_node, node_to_object)
  92.                     value_object = self._convert(node.value[key_node], node_to_object)
  93.                 
  94.                 value.append((key_object, value_object))
  95.             
  96.         
  97.         node.value = value
  98.         object = self.construct(node)
  99.         node_to_object[node] = object
  100.         return object
  101.  
  102.     
  103.     def construct(self, node):
  104.         return node.value
  105.  
  106.  
  107.  
  108. class Merge:
  109.     pass
  110.  
  111.  
  112. class Default:
  113.     pass
  114.  
  115.  
  116. class Loader(GenericLoader):
  117.     inf_value = float('inf')
  118.     nan_value = inf_value / inf_value
  119.     timestamp_expr = re.compile('(?P<year>\\d\\d\\d\\d)-(?P<month>\\d\\d)-(?P<day>\\d\\d)(?:(?:[Tt]|[ \\t]+)(?P<hour>\\d\\d):(?P<minute>\\d\\d):(?P<second>\\d\\d)(?:\\.(?P<micro>\\d+)?)?[ \\t]*(?:Z|(?P<zhour>[+-]\\d\\d)(?::(?P<zminute>\\d\\d))?)?)?')
  120.     merge_key = Merge()
  121.     default_key = Default()
  122.     non_ascii = []
  123.     for i in range(256):
  124.         ch = chr(i)
  125.         if ch.isalnum():
  126.             non_ascii.append(ch)
  127.             continue
  128.         non_ascii.append('_')
  129.     
  130.     non_ascii = ''.join(non_ascii)
  131.     python_bools = {
  132.         'True': True,
  133.         'False': False }
  134.     
  135.     class python_class:
  136.         pass
  137.  
  138.     
  139.     def find_constructor(self, node):
  140.         parts = []
  141.         if node.tag:
  142.             parts = node.tag.split(':')
  143.         
  144.         if parts:
  145.             if parts[0] == 'tag':
  146.                 parts.pop(0)
  147.                 if parts:
  148.                     if parts[0] == 'yaml.org,2002':
  149.                         parts.pop(0)
  150.                     elif parts[0] == 'python.yaml.org,2002':
  151.                         parts[0] = 'python'
  152.                     
  153.                 
  154.             elif parts[0] == 'x-private':
  155.                 parts[0] = 'private'
  156.             
  157.         
  158.         parts = [ part.translate(self.non_ascii) for part in parts ]
  159.         while parts:
  160.             method = 'construct_' + '_'.join(parts)
  161.             parts.pop()
  162.             continue
  163.             None if hasattr(self, method) else []
  164.  
  165.     
  166.     def construct(self, node):
  167.         if node.kind == 'map' and self.merge_key in node.value:
  168.             self.merge_maps(node)
  169.         
  170.         constructor = self.find_constructor(node)
  171.         if constructor:
  172.             return constructor(node)
  173.         else:
  174.             return node.value
  175.  
  176.     
  177.     def construct_null(self, node):
  178.         pass
  179.  
  180.     
  181.     def construct_bool_yes(self, node):
  182.         return True
  183.  
  184.     
  185.     def construct_bool_no(self, node):
  186.         return False
  187.  
  188.     
  189.     def construct_str(self, node):
  190.         
  191.         try:
  192.             value = unicode(node.value, 'utf-8')
  193.         except UnicodeDecodeError:
  194.             warnings.warn('scalar value is not utf-8', NotUnicodeInputWarning)
  195.             return node.value
  196.  
  197.         
  198.         try:
  199.             return value.encode('ascii')
  200.         except UnicodeEncodeError:
  201.             return value
  202.  
  203.  
  204.     
  205.     def construct_numeric_base60(self, num_type, node):
  206.         digits = [ num_type(part) for part in node.value.split(':') ]
  207.         digits.reverse()
  208.         base = 1
  209.         value = num_type(0)
  210.         for digit in digits:
  211.             value += digit * base
  212.             base *= 60
  213.         
  214.         return value
  215.  
  216.     
  217.     def construct_int(self, node):
  218.         return int(node.value)
  219.  
  220.     
  221.     def construct_int_hex(self, node):
  222.         return int(node.value, 16)
  223.  
  224.     
  225.     def construct_int_oct(self, node):
  226.         return int(node.value, 8)
  227.  
  228.     
  229.     def construct_int_base60(self, node):
  230.         return self.construct_numeric_base60(int, node)
  231.  
  232.     
  233.     def construct_float(self, node):
  234.         return float(node.value)
  235.  
  236.     construct_float_fix = construct_float
  237.     construct_float_exp = construct_float
  238.     
  239.     def construct_float_base60(self, node):
  240.         return self.construct_numeric_base60(float, node)
  241.  
  242.     
  243.     def construct_float_inf(self, node):
  244.         return self.inf_value
  245.  
  246.     
  247.     def construct_float_neginf(self, node):
  248.         return -(self.inf_value)
  249.  
  250.     
  251.     def construct_float_nan(self, node):
  252.         return self.nan_value
  253.  
  254.     
  255.     def construct_binary(self, node):
  256.         return node.value.decode('base64')
  257.  
  258.     
  259.     def construct_timestamp(self, node):
  260.         match = self.timestamp_expr.match(node.value)
  261.         values = match.groupdict()
  262.         for key in values:
  263.             if values[key]:
  264.                 values[key] = int(values[key])
  265.                 continue
  266.             values[key] = 0
  267.         
  268.         micro = values['micro']
  269.         if micro:
  270.             while 10 * micro < 1000000:
  271.                 micro *= 10
  272.         
  273.         stamp = datetime.datetime(values['year'], values['month'], values['day'], values['hour'], values['minute'], values['second'], micro)
  274.         diff = datetime.timedelta(hours = values['zhour'], minutes = values['zminute'])
  275.         return stamp - diff
  276.  
  277.     construct_timestamp_ymd = construct_timestamp
  278.     construct_timestamp_iso8601 = construct_timestamp
  279.     construct_timestamp_spaced = construct_timestamp
  280.     
  281.     def construct_merge(self, node):
  282.         return self.merge_key
  283.  
  284.     
  285.     def construct_default(self, node):
  286.         return self.default_key
  287.  
  288.     
  289.     def merge_maps(self, node):
  290.         maps = node.value[self.merge_key]
  291.         del node.value[self.merge_key]
  292.         if not isinstance(maps, list):
  293.             maps = [
  294.                 maps]
  295.         
  296.         maps.reverse()
  297.         maps.append(node.value.copy())
  298.         for item in maps:
  299.             node.value.update(item)
  300.         
  301.  
  302.     
  303.     def construct_omap(self, node):
  304.         omap = []
  305.         for mapping in node.value:
  306.             for key in mapping:
  307.                 omap.append((key, mapping[key]))
  308.             
  309.         
  310.         return omap
  311.  
  312.     
  313.     def construct_pairs(self, node):
  314.         pairs = []
  315.         for mapping in node.value:
  316.             for key in mapping:
  317.                 pairs.append((key, mapping[key]))
  318.             
  319.         
  320.         return pairs
  321.  
  322.     
  323.     def construct_set(self, node):
  324.         return Set(node.value)
  325.  
  326.     
  327.     def construct_python_none(self, node):
  328.         pass
  329.  
  330.     
  331.     def construct_python_bool(self, node):
  332.         return self.python_bools[node.value]
  333.  
  334.     
  335.     def construct_python_int(self, node):
  336.         return int(node.value)
  337.  
  338.     
  339.     def construct_python_long(self, node):
  340.         return long(node.value)
  341.  
  342.     
  343.     def construct_python_float(self, node):
  344.         return float(node.value)
  345.  
  346.     
  347.     def construct_python_complex(self, node):
  348.         return complex(node.value)
  349.  
  350.     
  351.     def construct_python_str(self, node):
  352.         return str(node.value)
  353.  
  354.     
  355.     def construct_python_unicode(self, node):
  356.         return unicode(node.value, 'utf-8')
  357.  
  358.     
  359.     def construct_python_list(self, node):
  360.         return node.value
  361.  
  362.     
  363.     def construct_python_tuple(self, node):
  364.         return tuple(node.value)
  365.  
  366.     
  367.     def construct_python_dict(self, node):
  368.         return node.value
  369.  
  370.     
  371.     def find_python_object(self, node):
  372.         full_name = node.tag.split(':')[3]
  373.         parts = full_name.split('.')
  374.         object_name = parts.pop()
  375.         module_name = '.'.join(parts)
  376.         if not module_name:
  377.             module_name = '__builtin__'
  378.         else:
  379.             __import__(module_name)
  380.         return getattr(sys.modules[module_name], object_name)
  381.  
  382.     
  383.     def find_python_state(self, node):
  384.         if node.kind == 'seq':
  385.             args = node.value
  386.             kwds = { }
  387.             state = { }
  388.         else:
  389.             args = node.value.get('args', [])
  390.             kwds = node.value.get('kwds', { })
  391.             state = node.value.get('state', { })
  392.         return (args, kwds, state)
  393.  
  394.     
  395.     def set_python_state(self, object, state):
  396.         if hasattr(object, '__setstate__'):
  397.             object.__setstate__(state)
  398.         else:
  399.             slotstate = { }
  400.             if isinstance(state, tuple) and len(state) == 2:
  401.                 (state, slotstate) = state
  402.             
  403.             if hasattr(object, '__dict__'):
  404.                 object.__dict__.update(state)
  405.             elif state:
  406.                 slotstate.update(state)
  407.             
  408.             for key, value in slotstate.items():
  409.                 setattr(object, key, value)
  410.             
  411.  
  412.     
  413.     def construct_python_name(self, node):
  414.         return self.find_python_object(node)
  415.  
  416.     
  417.     def construct_python_module(self, node):
  418.         module_name = node.tag.split(':')[3]
  419.         __import__(module_name)
  420.         return sys.modules[module_name]
  421.  
  422.     
  423.     def construct_python_object(self, node):
  424.         cls = self.find_python_object(node)
  425.         if type(cls) is type(self.python_class):
  426.             if hasattr(cls, '__getnewargs__'):
  427.                 object = cls()
  428.             else:
  429.                 object = self.python_class()
  430.                 object.__class__ = cls
  431.         else:
  432.             object = cls.__new__(cls)
  433.         self.set_python_state(object, node.value)
  434.         return object
  435.  
  436.     
  437.     def construct_python_new(self, node):
  438.         cls = self.find_python_object(node)
  439.         (args, kwds, state) = self.find_python_state(node)
  440.         if type(cls) is type(self.python_class):
  441.             object = cls(*args, **kwds)
  442.         else:
  443.             object = cls.__new__(cls, *args, **kwds)
  444.         self.set_python_state(object, state)
  445.         return object
  446.  
  447.     
  448.     def construct_python_apply(self, node):
  449.         constructor = self.find_python_object(node)
  450.         (args, kwds, state) = self.find_python_state(node)
  451.         object = constructor(*args, **kwds)
  452.         self.set_python_state(object, state)
  453.         return object
  454.  
  455.  
  456.  
  457. def parse(source, Loader = Loader, **parameters):
  458.     loader = Loader(source, **parameters)
  459.     return loader.parse()
  460.  
  461.  
  462. def load(source, Loader = Loader, **parameters):
  463.     loader = Loader(source, **parameters)
  464.     return loader.load()
  465.  
  466.  
  467. def parse_documents(source, Loader = Loader, **parameters):
  468.     loader = Loader(source, **parameters)
  469.     while True:
  470.         node = loader.parse()
  471.         if loader.eof:
  472.             break
  473.         
  474.         yield node
  475.  
  476.  
  477. def load_documents(source, Loader = Loader, **parameters):
  478.     loader = Loader(source, **parameters)
  479.     while True:
  480.         object = loader.load()
  481.         if loader.eof:
  482.             break
  483.         
  484.         yield object
  485.  
  486.